"""
google 翻译接口
"""
from googletrans import Translator
import asyncio


class GoogleTranslator(object):

    def __init__(self):
        self.translator = Translator()


    async def translate_text(self, text: str, from_lan: str=None, to_lan=None):
        if from_lan is None:
            result = await self.translator.translate(text, dest=to_lan)
        elif to_lan is None:
            result = await self.translator.translate(text, src=from_lan)
        else:
            result = await self.translator.translate(text, src=from_lan, dest=to_lan)
        return result.text


if __name__ == '__main__':
    translator = GoogleTranslator()
    print(asyncio.run(translator.translate_text('안녕하세요.', to_lan='ja')))
